package com.guosheng.service;

import com.alibaba.fastjson.JSONObject;
import com.google.common.annotations.VisibleForTesting;
import com.guosheng.dao.EmpDao;
import com.guosheng.entity.EmpEntity;
import com.guosheng.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EmpOnService {

    @Autowired
    private EmpDao empDao;

    @Autowired
    private RedisTemplate redisTemplate;

    public void redisTest(){
        redisTemplate.delete("studentone");
        List<Student> list = new ArrayList<>();
        for (int i=0;i<= 10; i++){
            Student s = new Student();
            s.setId(i);
            list.add(s);
        }
        JSONObject json = new JSONObject();
        Map<String,String> map = new HashMap<>();
        map.put("student1",json.toJSONString(list));
        map.put("currentTimeStamp",String.valueOf(new Date().getTime()/1000));
        redisTemplate.opsForHash().putAll("studentone",map);
        Map<String,String> result = redisTemplate.opsForHash().entries("studentone");
        System.out.println(result);
        Map<String,String> test = redisTemplate.opsForHash().entries("hahahahaha");
        System.out.println(test);
        Map<String,String> map2 = new HashMap<>();
        map2.put("studenttwo","studenttwo");
        map2.put("studentthree","studentthree");
        redisTemplate.opsForHash().putAll("studentone",map2);
        test = redisTemplate.opsForHash().entries("studentone");
        System.out.println(test);
        redisTemplate.opsForHash().delete("studentone","studenttwo");
        test = redisTemplate.opsForHash().entries("studentone");
        System.out.println(test);
    }

    public List<EmpEntity> searchEmpList(Map<String,Object> param){
        return empDao.searchEmpList(param);
    }

    public void testSet(){
        redisTemplate.delete("luckyCodePool");
        redisTemplate.opsForSet().add("luckyCodePool","111","222","333","444","555");
        System.out.println(redisTemplate.opsForSet().members("luckyCodePool"));
        System.out.println(redisTemplate.opsForSet().isMember("luckyCodePool","222"));
        System.out.println(redisTemplate.opsForSet().remove("luckyCodePool","666"));
        System.out.println(redisTemplate.opsForSet().members("luckyCodePool"));
        System.out.println(redisTemplate.opsForSet().members("luckyCodePool"));
        System.out.println(redisTemplate.opsForSet().pop("luckyCodePool"));
        System.out.println(redisTemplate.opsForSet().members("luckyCodePool"));
        System.out.println(redisTemplate.opsForSet().size("luckyCodePool"));
        String[] strs = new String[]{"aaa","bbb","ccc","ddd"};
        redisTemplate.opsForSet().add("luckyCodePool",strs);
        System.out.println(redisTemplate.opsForSet().members("luckyCodePool"));

    }


}
